package com.culturalCenter.dataCenter.globalconfig;

import com.culturalCenter.dataCenter.globalconfig.exception.VersionException;
import org.springframework.web.servlet.mvc.condition.RequestCondition;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 自定义url匹配逻辑
 * Created on 2019/4/18 14:07.
 *
 * @author Child
 */
public class ApiVersionCondition implements RequestCondition<ApiVersionCondition> {

    /**
     * 路径中版本的前缀， 这里用 /v[1-9]/的形式
     */
    private final static Pattern VERSION_PREFIX_PATTERN = Pattern.compile("v(\\d+)/");

    /**
     * api的版本
     */
    private int[] apiVersion;

    public ApiVersionCondition(String[] version) {
        Pattern p = Pattern.compile("\\d+");
        int forCount = 0;
        int[] intVersions = new int[version.length];
        for (String api : version) {
            Matcher m = p.matcher(api);
            if (m.find()) {
                intVersions[forCount] = Integer.parseInt(m.group(0));
                forCount++;
            }
            Arrays.sort(intVersions);
            this.apiVersion = intVersions;
        }


    }

    private static int getMax(int[] intArray) {
        // 假设数组中的第一个元素的值最大
        int max = intArray[0];
        // 从数组的第二个元素开始遍历
        for (int i = 1; i < intArray.length; i++) {
            // 如果max小于数组中的值时
            if (max < intArray[i]) {
                // 将数组中的值赋值给max
                max = intArray[i];
            }

        }
        return max;
    }

    /**
     * 将不同的筛选条件合并
     *
     * @param apiVersionCondition the condition to combine with.
     *
     * @return
     */
    @Override
    public ApiVersionCondition combine(ApiVersionCondition apiVersionCondition) {
        // 采用最后定义优先原则，则方法上的定义覆盖类上面的定义
        String[] strVersion = new String[apiVersionCondition.getApiVersion().length];
        int count = 0;
        for (int intVersion : apiVersionCondition.getApiVersion()) {
            strVersion[count] = "v" + intVersion;
            count++;
        }
        return new ApiVersionCondition(strVersion);
    }

    /**
     * 根据request查找匹配到的筛选条件
     */
    @Override
    public ApiVersionCondition getMatchingCondition(HttpServletRequest httpServletRequest) throws VersionException {
        // String version = httpServletRequest.getHeader("Accept-version");
        // return null;
        Matcher m = VERSION_PREFIX_PATTERN.matcher(httpServletRequest.getRequestURI());
        if (m.find()) {
            // 如果请求的版本号大于配置版本号，则满足
            Integer version = Integer.valueOf(m.group(1));
            for (int i = 0; i < this.apiVersion.length; i++) {
                if (version >= this.apiVersion[0]) {
                    return this;
                }
            }
        }
        throw new VersionException("根据规则找不到对应请求");
        // return null;
    }

    /**
     * 不同筛选条件比较,用于排序
     */
    @Override
    public int compareTo(ApiVersionCondition apiVersionCondition, HttpServletRequest httpServletRequest) {
        // return 0;
        // 优先匹配最新的版本号
        return getMax(apiVersionCondition.getApiVersion());
    }

    public int[] getApiVersion() {
        return apiVersion;
    }

}
